#
# add_album.py - code to add a photo album to the users profile in the datastore
#
# Copyright 2008-2009 Autelic Association (http://www.autelic.org)
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi, sys, os, logging, string
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app
from google.appengine.ext import db
import wsgiref.handlers

import loggedin.profile
import pages
from google.appengine.api import users

import gdata.service
import gdata.alt.appengine
import gdata.photos.service

####################### add_album class ###########################
class add_album(webapp.RequestHandler):
  def post(self):
    album_error_message=''
    # get the parameters from the form
       
    # There's got to be a more elegant way of getting more than 3 paramters...
    values = dict((k, cgi.escape(self.request.get(k))) for k in (
                  'photo_service',
                  'user_id',
                  'album_id',
                  'album_name',
                  'auth_key',
                  'source',
                  'full_url'))

    source = values['source'].strip()
    full_url = values['full_url'].strip()

    if source == 'URL':          # from an arbitrary URL
       if not full_url:          # but none in form
          album_error_message = 'Source specified as URL, but no URL supplied'
    elif source == 'RSS':        # RSS Feed
       if not full_url:           # but no url for feed in form
          album_error_message = 'No URL specified for RSS Feed'
    elif source == 'PicasaWeb':  # Photo service specification
       # check the other paramaters describing access to the photo service
       # TODO check them properly
       # TODO check we can then actually access the album...
       album = loggedin.profile.SharedAlbum()
       album.source = str(source)
       album.full_url = full_url
       album.user_id = values['user_id'].strip()
       album.album_id = values['album_id'].strip()
       album.album_name = values['album_name'].strip()
       album.auth_key = values['auth_key'].strip()

       # load user profile from the Datastore
       q = db.GqlQuery("SELECT * FROM Profile WHERE user = :1",
                       users.get_current_user())
       profile = q.get()

       # store album
       key = db.put(album)

       # Add the album to the list of the users shared albums
       profile.shared_albums.append(key)
       
       # store into the Datastore
       db.put(profile)
    else:
       album_error_message = 'Invalid source specified.'

    self.redirect('/in/cpanel?album_error_message=%s' 
                     % album_error_message)
    
####### Main
def main(argv):
  application = webapp.WSGIApplication(
    [('/in/add_album', add_album)],
    debug=True)
    
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == "__main__":
  main(sys.argv)
